package com.gh.ggkt.vod.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.ggkt.model.vod.Subject;
import com.atguigu.ggkt.vo.vod.SubjectEeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gh.exception.GgktException;
import com.gh.ggkt.vod.listen.SubjectExcelListener;
import com.gh.ggkt.vod.mapper.SubjectMapper;
import com.gh.ggkt.vod.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Eric Fang
 * @since 2022-09-10
 */
@Slf4j
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public List<Subject> listSubject(Long id) {
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getParentId, id);
        List<Subject> subjectList = baseMapper.selectList(queryWrapper);
        subjectList = subjectList.stream().map(item -> {
            Long itemId = item.getId();
            LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Subject::getParentId, itemId);
            Integer count = baseMapper.selectCount(wrapper);
            if (count > 0) {
                item.setHasChildren(true);
            }
            return item;
        }).collect(Collectors.toList());
        return subjectList;
    }

    @Override
    public void exportExcelData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //查询课程分类信息
            List<Subject> subjectList = baseMapper.selectList(null);
            List<SubjectEeVo> subjectEeVos = new ArrayList<>();
            subjectEeVos = subjectList.stream().map(item -> {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
                BeanUtils.copyProperties(item, subjectEeVo);
                return subjectEeVo;
            }).collect(Collectors.toList());
            //easyExcel写操作
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
                    .sheet("课程分类")
                    .doWrite(subjectEeVos);
        } catch (Exception e) {
            log.error("导出失败");
        }

    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SubjectEeVo.class, new SubjectExcelListener(baseMapper))
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            throw new GgktException(20001, "导入发生异常");
        }
    }
}
